package cn.comon.app.report;

import java.io.*;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import org.json.JSONObject;

import cn.comon.app.scan.util.UtilTools;

public class TencentAnalysisTMP{
    public static void sendRequest(String id, String secureKey) {
        try {
//            URL request = new URL("http://localhost:8080/appScan/scan/scan.do?userName=comon");
            URL request = new URL("http://open.scan.qq.com/api/scansoft?id="+id);
            // 主机返回状态。
            String result = "";
            // 主机返回状态详情
            String reason = "";
            HttpURLConnection connection = (HttpURLConnection)request.openConnection();

            //构造post请求
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            //设置连接主机超时（单位：毫秒）。1000 毫秒= 1秒
            connection.setConnectTimeout(30000);
            //设置从主机读取数据超时（单位：毫秒）
            connection.setReadTimeout(30000); 
            connection.connect();

            String scanParam = "{\"scanlist\":[{\"sid\":\"123\", \"url\":\"http://dl.test.com/test.apk\", \"md5\":\"3d41f29d762ec547bfa4b42f57f3dc7c\"}]}";

            //计算authkey
            String authKey = UtilTools.getStringMd5(scanParam + id + secureKey);
            String post = "authkey=" + authKey + "&response=" + scanParam;
            System.out.println("<----------------reqeust begin---------------->");
            System.out.println(post);
            System.out.println("<----------------reqeust end---------------->"); 

            DataOutputStream writeStream = new DataOutputStream(connection.getOutputStream());
            writeStream.writeBytes(post); 
            writeStream.flush();
            writeStream.close(); 
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line="";
            System.out.println("<----------------response begin---------------->");
            while ((line = reader.readLine()) != null){
               //eg:{"result":"error", "reason":"invalid ip"}
                System.out.println(line);
                JSONObject jSONObject= new JSONObject(line);
                result = (String)jSONObject.get("result");
                reason = (String)jSONObject.get("reason");
                System.out.println(result);
                System.out.println(reason);
            }
            System.out.println("<----------------response end---------------->");
            if ("ok".equals(result.toLowerCase())) {
                // 主机响应正常，举报信息正常发送完毕。DB状态更新。
            } else {
                // 主机响应异常，举报信息未正常发送。DB状态更新。
            }
            reader.close();
            connection.disconnect();
        } catch (ConnectException connectException) {
            // 对方服务器拒绝被连接（连接不上对方服务器）
            connectException.printStackTrace();
        } catch (SocketTimeoutException socketTimeoutException) {
            // 连接主机超时，从主机读取数据超时。
            socketTimeoutException.printStackTrace();
        } catch (IOException iOException) {
            iOException.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            sendRequest("12321", "OUIaz8gwGQTTb1kZ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}